package com.raycloud.action.user;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.raycloud.action.common.BaseAction;
import com.raycloud.common.Constants;
import com.raycloud.common.ScopeConstants;
import com.raycloud.dao.dbhash.DbHashUtil;
import com.raycloud.domain.Msg;
import com.raycloud.domain.MsgRelation;
import com.raycloud.domain.User;
import com.raycloud.manager.MsgMgr;
import com.raycloud.util.ShortUrl;

public class MsgAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = Logger.getLogger(MsgAction.class);

	private MsgMgr msgMgr;
	
	private String msg;
	private String transferMsgId;
	
	/**
	 * 原创微博
	 * @return
	 * @throws Exception
	 */
	public String original() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		try {
			User user = (User) session.get(ScopeConstants.USER);
			Msg msg = fillMsg(user, Constants.ORIGINAL);
			msgMgr.publishOriginalMsg(msg, user);
			out.println(1);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			out.println("发表失败");
		} finally {
			if(out != null) {
				out.close();
			}
		}
		return null;
	}
	
	public String transfer() throws Exception {
		User user = (User) session.get(ScopeConstants.USER);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		try {
			if(StringUtils.isBlank(this.msg)) {
				this.msg = "转发微博";
			}
			Msg msg = fillMsg(user, Constants.FORWARD);
			Msg transferMsg = msgMgr.getMsgById(transferMsgId);
			if(transferMsg == null) {
				out.println("您所转发的消息可能已经删除");
				return null;
			}
			
			MsgRelation msgRelation = fillMsgRelation(msg, transferMsg);
			msgMgr.publishTransferMsg(msg, msgRelation, user);
			
			out.println(1);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			out.println("转发失败");
		} finally {
			if(out != null) {
				out.close();
			}
		}
		return null;
	}
	
	private Msg fillMsg(User user, int type) {
		Msg msg = new Msg();
		msg.setAddTime(new Date());
		msg.setContent(this.msg);
		msg.setType(type);
		msg.setUserId(user.getId());
		msg.setMsgId(ShortUrl.getKey());
		msg.setNick(user.getUserName());
		return msg;
	}
	
	private MsgRelation fillMsgRelation(Msg msg, Msg transferMsg) {
		MsgRelation msgRelation = new MsgRelation();
		msgRelation.setAddTime(new Date());
		msgRelation.setDbId(DbHashUtil.getMsgRelationDBId(msg.getUserId()));
		msgRelation.setType(Constants.FORWARD);
		msgRelation.setReferencedId(transferMsg.getUserId());
		msgRelation.setReferencedMsgId(transferMsg.getMsgId());
		msgRelation.setReferenceId(msg.getUserId());
		msgRelation.setReferenceMsgId(msg.getMsgId());
		msgRelation.setPageIndex(transferMsg.getTransferedCount() % Constants.MSG_FETCH_SIZE);
		return msgRelation;
	}
	
	public void setMsgMgr(MsgMgr msgMgr) {
		this.msgMgr = msgMgr;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setTransferMsgId(String transferMsgId) {
		this.transferMsgId = transferMsgId;
	}

}
